<?php

namespace App\Http\Controllers\API\Pad;

use App\Http\Controllers\AppBaseController;
use App\Models\ParkInfo;
use App\Models\TaxiRelease;
use App\Models\TaxiReleaseInfo;
use GuzzleHttp\Client;
use Illuminate\Http\Request;

class TaxiController extends AppBaseController
{
    /**
     * 出租车放行提交
     *
     * @param Request $request
     */
    public function post(Request $request)
    {
        $this->validate($request, [
                'out_park_code' => 'required',
                'in_park_code' => 'required',
                'out_arm_code' => 'required',
                'out_number' => 'required'
            ],[],
                [
                    'out_park_code'=>'蓄车池id',
                    'out_arm_code'=>'出口id',
                    'in_park_code'=>'接客区id',
                    'out_number'=>'放行数量'
                ]

        );

        \Log::info('taxi request');
        \Log::info($request->all());

        $model = new TaxiRelease($request->all());
        $model->user_id = $request->user()->id;
        $uuid = uniqid();
        $model->uuid = $uuid;
        $model->save();

        $http = new Client(['base_uri' => config('custom.car_now_auth_api_url')]);

        $res = $http->request('POST', 'taxi_dispatch', ['form_params' =>
            [
                'park_code' => $request->get('out_park_code'),
                'arm_code' => $request->get('out_arm_code'),
                'protocol' => \GuzzleHttp\json_encode([
                    'out_vpr_num' => $request->get('out_number'),
                    'target_name' => ParkInfo::find($request->get('in_park_code'))->parking_name,
                    'out_arm_id' => $request->get('out_arm_code'),
                    'mark_id' => $model->uuid
                ])
            ]]);
//        $this->log($res->getBody()->getContents());
        $cont = json_decode($res->getBody()->getContents(), true);
        \Log::info($res->getStatusCode());
        \Log::info(json_encode($cont));
        if($res->getStatusCode()!=200 || $cont['code']!=0) {
            return $this->sendError('操作错误，请稍后重试'.$cont['err']);
        }
        return $this->sendResponse(['uuid'=>$uuid],'开闸成功');

    }


    /**
     * 出租车放行查询
     *
     * @param $id
     * @param Request $request
     */
    public function query($id,Request $request)
    {
        $model = TaxiRelease::where('uuid',$id)->firstOrFail();

        $nowCount = TaxiReleaseInfo::where('uuid',$id)->withTrashed()->count();


        return $this->sendResponse(['out_number'=>$model->out_number,'now_out_number'=>$nowCount],'查询成功');
    }

    /**
     * 出租车出口免费抬杆
     *
     * @param Request $request
     */
    public function freeRelease(Request $request)
    {
        $this->validate($request, [
                'out_park_code' => 'required',
                'out_arm_code' => 'required'
            ],[],
            [
                'out_park_code'=>'蓄车池id',
                'out_arm_code'=>'出口id'
            ]
        );

        $http = new Client(['base_uri' => config('custom.car_auth_api_url')]);

        //删除授权
        $res = $http->request('POST', 'raise_pole', ['form_params' =>
            [
                'park_code' => $request->out_park_code,
                'arm_code' => $request->out_arm_code,
                'type' => 1,
                'gatway'=>1
            ]]);


        \Log::info($res->getStatusCode().'---1111111');
        if ($res->getStatusCode() != 200) {
            return $this->sendError('操作错误，请稍后重试');
        };

        $response = json_decode($res->getBody()->getContents());

        \Log::info($response->code.'---22222222');
        if (!$response || $response->code != 0) {
            return $this->sendError('操作错误，请稍后重试');
        }

        return $this->sendResponse([],'操作成功');
    }
}
